import testNapi from 'libentry.so'
import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';

var workerPort : ThreadWorkerGlobalScope = worker.workerPort;

/**
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessage = function(e : MessageEvents) {
  switch (e.data.type as number) {
    case 0:
      // console.log('[worker] receive message from main to create function')
      testNapi.createThreadSafeFunction("testWorkerMainSafe", (value1: string, value2: string) => {
        // console.log("ArkTS recv string: ", (value1 + value2));
        let res: string = workerPort.callGlobalCallObjectMethod("myObj", "getMessage", 0, value1, value2) as string;
        // console.log("worker: ", res);
        return res;
      })
  }
}

/**
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessageerror = function(e : MessageEvents) {
}

/**
* Defines the event handler to be called when an exception occurs during worker execution.
* The event handler is executed in the worker thread.
*
* @param e error message
*/
workerPort.onerror = function(e : ErrorEvent) {
}